// pages/login/login.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_type:1,
    device:'',
    name:'',
    pass:''
  },
  //忘记密码页面
  fotget(){
    var user_type = this.data.user_type;

    app.navTo('../forget/forget?type='+user_type)
  },
  register(){
    var user_type = this.data.user_type;

    app.navTo('../register/register?type='+user_type)
  },
  inputName:function(e){
    var name = e.detail.value;
    this.setData({
      name:name
    })
  },
  inputPass:function(e){
    var pass = e.detail.value;
    this.setData({
      pass: pass
    })
  },
  onGotUserInfo(e){
 
    var that = this;
    var name = that.data.name;
    var pass = that.data.pass;
    var user_type = that.data.user_type;
    var device =that.data.device;
    if (!name) {
      wx.showToast({
        title: '用户名不能为空',
        icon: 'none'
      })
      return false;
    } else if (!pass) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none'
      })
      return false;
    }else{
      var user_data = e.detail;
      console.log("login")
      wx.login({
        success(ress) {
          console.log(ress)
          var param = {
            code: ress.code,
            rawData: user_data.rawData,
            signature: user_data.signature,
            encryptedData: user_data.encryptedData,
            iv: user_data.iv,
            type: 1, 
            name: name, 
            pass: pass, 
            user_type: user_type,
            device: device
          };
          wx.showLoading({})
          var that = this;
          wx.showLoading();
          util.commonRequest('userlogin', 'post', param, function (res) {
            wx.hideLoading();
            if (res.data.code == 200) {
            wx.setStorageSync('userInfo',res.data.data)
            wx.showToast({
                title: '登录成功',
                icon: 'success',
                duration: 1500
              })
              setTimeout(function () {

                wx.reLaunch({
                  url: '../index/index'
                })

              }, 1500);
            } else {
              wx.showToast({
                icon: 'loading',
                title: res.data.msg,
              })
            }
          });
        }
      })


 
     
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = options.type;
    this.setData({
      user_type:type
    })
    var that = this;
    wx.getSystemInfo({
      success(res) {
        var device = "手机品牌:" + res.brand + " 手机型号：" + res.model + "操作系统:" + res.system;
        that.setData({
          device:device
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})